# Created by yang

from django.db.models import Q
from django.urls import reverse
from django.utils.safestring import mark_safe
from . import models
from arya.service import sites

# ----------------------部门表----------------------
class DepartmentConfig(sites.AryaConfig):
    list_display = ["title", ]


sites.site.register(models.Department, DepartmentConfig)

# ----------------------员工表----------------------
class UserInfoConfig(sites.AryaConfig):
    list_display = ["name", ]


sites.site.register(models.UserInfo, UserInfoConfig)

# ----------------------课程表----------------------
class CourseConfig(sites.AryaConfig):
    list_display = ["name", ]


sites.site.register(models.Course, CourseConfig)

# ----------------------校区表----------------------
class SchoolConfig(sites.AryaConfig):
    list_display = ["title", ]


sites.site.register(models.School, SchoolConfig)

# ----------------------班级表----------------------
class ClassListConfig(sites.AryaConfig):
    def course_display(self, obj=None, is_header=False):
        if is_header:
            return "班级"
        else:
            tpl = "{}({}期)".format(obj.course.name, obj.semester)
            return tpl

    def teachers_display(self, obj=None, is_header=False):
        if is_header:
            return "任课老师"
        else:
            teachers = obj.teachers.all()
            result = []
            for teacher in teachers:
                tpl = "<span>{}</span>".format(teacher.name)
                result.append(tpl)
            return mark_safe(" ".join(result))

    list_display = ["school", course_display, "tutor", teachers_display, "price", ]


sites.site.register(models.ClassList, ClassListConfig)


# ----------------------客户表----------------------
class CustomerConfig(sites.AryaConfig):
    search_list = ["name", "qq"]  # 模糊搜索,将在name和qq这两列进行模糊搜索

    def courses_display(self, obj=None, is_header=False):
        if is_header:
            return "咨询课程"
        else:
            courses = obj.course.all()
            result = []
            for course in courses:
                tpl = "<span>{}</span>".format(course.name)
                result.append(tpl)
            return mark_safe(" ".join(result))

    def gender_display(self, obj=None, is_header=False):
        if is_header:
            return "性别"
        else:
            return obj.get_gender_display()

    def consult_record(self, obj=None, is_header=False):
        if is_header:
            return "跟进记录"
        else:
            base_url = reverse("{0}:{1}_{2}_changelist".format(self.site.namespace, self.app_label, "consultrecord"))
            query = self.request.GET.get(self.change_filter_name)
            url1 = "{0}?{1}".format(base_url, query if query else "")

            url = "{}&{}={}".format(url1, "customer", obj.pk)

            return mark_safe("<a href={}>跟进记录</a>".format(url))

    list_display = ["name", gender_display, "qq", "consultant", courses_display,consult_record]

    list_filter = [
        # sites.FilterOption("name",False,lambda x:x.name,lambda x:x.name),#这种配置无意义
        sites.FilterOption("consultant", False, condition=Q(depart=1)),  # 传条件以显示筛选项
        sites.FilterOption("course", True),  # 咨询课程
        sites.FilterOption("gender", False),  # choices

    ]


sites.site.register(models.Customer, CustomerConfig)

# ----------------------客户跟进记录表----------------------
class ConsultRecordConfig(sites.AryaConfig):
    def date(self, obj=None, is_header=False):
        if is_header:
            return "跟进日期"
        else:
            return obj.date.strftime("%Y-%m-%d")

    list_display = ["customer","consultant","note",date]

sites.site.register(models.ConsultRecord, ConsultRecordConfig)

# ----------------------缴费记录表----------------------
class PaymentRecordConfig(sites.AryaConfig):

    def pay_type_choices(self, obj=None, is_header=False):
        if is_header:
            return "费用类型"
        else:
            return obj.get_pay_type_display()

    def date(self, obj=None, is_header=False):
        if is_header:
            return "交款日期"
        else:
            return obj.date.strftime("%Y-%m-%d")

    list_display = ["customer","class_list",pay_type_choices,"paid_fee","consultant",date]

sites.site.register(models.PaymentRecord, PaymentRecordConfig)

# ----------------------学生表----------------------
class StudentConfig(sites.AryaConfig):
    def class_list(self,obj=None,is_header=False):
        if is_header:
            return "已报班级"
        else:
            clazz_list = obj.class_list.all()
            result = []
            for clazz in clazz_list:
                tpl = "<span>{}</span>".format(clazz)
                result.append(tpl)
            return mark_safe(" ".join(result))

    list_display = ["customer","username",class_list]
sites.site.register(models.Student, StudentConfig)

# ----------------------上课记录表----------------------
class CourseRecordConfig(sites.AryaConfig):
    def date(self, obj=None, is_header=False):
        if is_header:
            return "上课日期"
        return str(obj.date)
    list_display = ["course","day_num","teacher","course_title",date,]

    def init_course_record(self,request):
        """初始化上课记录,当讲师添加上课记录后,当前班级中每个学生添加一条学习记录"""
        pk_list = request.POST.getlist('pk')
        course_records = self.model_class.objects.filter(pk__in=pk_list)#所有的上课记录对象
        for course_record in course_records:
            students = course_record.course.student_set.all()
            from django.db import transaction
            with transaction.atomic():
                result=[]
                for student in students:
                    if models.StudyRecord.objects.filter(course_record=course_record,student=student):
                        continue
                    obj = models.StudyRecord(course_record=course_record,student=student)
                    result.append(obj)
                models.StudyRecord.objects.bulk_create(result)


    init_course_record.short_description = "初始化"

    actions = [init_course_record,]

sites.site.register(models.CourseRecord, CourseRecordConfig)

# ----------------------学习记录表----------------------
class StudyRecordConfig(sites.AryaConfig):
    list_display = ["course_record","student","score"]

sites.site.register(models.StudyRecord, StudyRecordConfig)
